<template>
  <div class="my-courses-container">

    <div class="top-bar">
      <el-input placeholder="请输入课程名搜索" v-model="searchQuery" class="search-input">
        <el-button slot="append" icon="el-icon-search"></el-button>
      </el-input>
      <el-select v-model="sortOrder" placeholder="排序" class="sort-select">
        <el-option label="ID升序" value="asc"></el-option>
        <el-option label="ID降序" value="desc"></el-option>
      </el-select>
      <el-button type="primary" @click="goToAllCourses">所有课程</el-button>
    </div>

    <el-table :data="filteredMyCourses" style="width: 100%">
      <el-table-column prop="pkCid" label="ID" width="100"></el-table-column>
      <el-table-column prop="courseName" label="课程名" width="180"></el-table-column>
      <el-table-column prop="startTime" label="上课时间" width="180"></el-table-column>
      <el-table-column prop="credit" label="学分" width="100"></el-table-column>
      <el-table-column prop="hours" label="学时" width="100"></el-table-column>
      <el-table-column prop="teacher" label="授课教师" width="180"></el-table-column>
      <el-table-column prop="category" label="类别" width="120"></el-table-column>
      <el-table-column label="操作" width="120">
        <template slot-scope="scope">
          <el-button type="text" @click="unselectCourse(scope.row)">退选</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="filteredMyCourses.length">
    </el-pagination>

    <!-- 退选模态框 -->
    <el-dialog title="退选" :visible.sync="dialogVisible" width="30%">
      <el-form :model="selectedCourse" label-width="120px">
        <el-form-item label="课程ID">
          <el-input v-model="selectedCourse.pkCid" disabled></el-input>
        </el-form-item>
        <el-form-item label="课程名">
          <el-input v-model="selectedCourse.courseName" disabled></el-input>
        </el-form-item>
        <el-form-item label="上课老师">
          <el-input v-model="selectedCourse.teacher" disabled></el-input>
        </el-form-item>
        <el-form-item label="上课时间">
          <el-input v-model="selectedCourse.startTime" disabled></el-input>
        </el-form-item>
        <el-form-item label="学分">
          <el-input v-model="selectedCourse.credit" disabled></el-input>
        </el-form-item>
        <el-form-item label="类别">
          <el-input v-model="selectedCourse.category" disabled></el-input>
        </el-form-item>
        <el-form-item label="学时">
          <el-input v-model="selectedCourse.hours" disabled></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmUnselectCourse">退 选</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  name: 'MyCourses',
  data() {
    return {
      searchQuery: '',
      sortOrder: 'asc',
      currentPage: 1,
      pageSize: 10,
      dialogVisible: false,
      selectedCourse: {},
      myCourses: []
    };
  },
  computed: {
    filteredMyCourses() {
      let filtered = this.myCourses.filter(course => {
        return course.courseName.toLowerCase().includes(this.searchQuery.toLowerCase());
      });
      if (this.sortOrder === 'asc') {
        filtered.sort((a, b) => a.pkCid - b.pkCid);
      } else if (this.sortOrder === 'desc') {
        filtered.sort((a, b) => b.pkCid - a.pkCid);
      }
      return filtered;
    }
  },

  created(){
    this.fetchMyCourses()
  },

  methods: {
    fetchMyCourses(){
      //获取userid
      let userId = localStorage.getItem("userId")
      axios.get("/api/course/byUserId?userId="+userId).then(response => {
        console.log(response.data)
        this.myCourses = response.data
      }).catch()
    },

    goToAllCourses() {
      this.$router.push({ name: 'courses' });
    },
    unselectCourse(course) {
      this.selectedCourse = { ...course };
      this.dialogVisible = true;
    },
    confirmUnselectCourse() {
      //获取用户ID和课程ID
      let userId = localStorage.getItem("userId")
      let courseId = this.selectedCourse.pkCid

      axios.get("/api/course/deleteById",{params:{
        userId,
        courseId
      }}).then(response => {
        this.$message.success("退选成功")
        this.dialogVisible = false;
        this.fetchMyCourses()

      }).catch(err => {
        this.$message.error(err.response.data)
      })

      // 处理退选逻辑
      this.dialogVisible = false;
    },
    
    handleSizeChange(val) {
      this.pageSize = val;
    },
    handleCurrentChange(val) {
      this.currentPage = val;
    }
  }
};
</script>

<style scoped>
.my-courses-container {
  padding: 20px;
}

.top-bar {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}

.search-input {
  width: 300px;
  margin-right: 20px;
}

.sort-select {
  width: 120px;
  margin-right: 20px;
}
</style>
